package cn.shyouchi.wuguanjia.android.ui.meetting.plan;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.shyouchi.wuguanjia.android.R;
import cn.shyouchi.wuguanjia.android.api.meet.MeetApi;
import cn.shyouchi.wuguanjia.android.base.BaseActivity;
import cn.shyouchi.wuguanjia.android.model.BaseResult;
import cn.shyouchi.wuguanjia.android.subscribers.ProgressSubscriber;
import cn.shyouchi.wuguanjia.android.widget.TopBar;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class PlanMeetActivity extends BaseActivity {

    @BindView(R.id.topBar)
    TopBar mTopBar;
    @BindView(R.id.rb_start)
    RadioButton mRbStart;
    @BindView(R.id.rb_begin)
    RadioButton mRbBegin;
    @BindView(R.id.rb_end)
    RadioButton mRbEnd;
    @BindView(R.id.rg_group)
    RadioGroup mRgGroup;
    @BindView(R.id.rv_meets)
    RecyclerView mRvMeets;


    PlanMeetAdapter mPlanMeetAdapter;

    List<PlanMeetEntity> mPlanMeetEntities = new ArrayList<>();

    MeetApi mMeetApi;

    @Override
    public int initContentView() {
        return R.layout.activity_plan_meet;
    }

    @Override
    public void initInjector() {
        mMeetApi = new MeetApi(mContext);

    }

    @Override
    public void initUiAndListener() {
        ButterKnife.bind(this);
        mTopBar.setTitle(getTitle().toString());
        mTopBar.setLeftIcon(R.mipmap.ic_back);
        setSupportActionBar(mTopBar.getToolbar());

//
//        for (int i = 0; i < 20; i++) {
//            mPlanMeetEntities.add(new PlanMeetEntity());
//        }


        mPlanMeetAdapter = new PlanMeetAdapter(mPlanMeetEntities);
        mRvMeets.setAdapter(mPlanMeetAdapter);
        mRvMeets.setLayoutManager(new LinearLayoutManager(mContext));

        fetchMeets();

        mRgGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                fetchMeets();
            }
        });


    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return false;
    }

    @Override
    protected boolean isApplyStatusBarColor() {
        return true;
    }

    private void fetchMeets() {
        String path = "start";
        if (mRbBegin.isChecked()) {
            path = "start";
        } else if (mRbStart.isChecked()) {
            path = "notStart";
        } else if (mRbEnd.isChecked()) {
            path = "end";
        }

        mMeetApi.fetchMeets(path)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber<BaseResult<List<PlanMeetEntity>>>(mContext) {

                    @Override
                    public void onStart() {
                        super.onStart();
                        mPlanMeetEntities.clear();
                    }

                    @Override
                    public void onNext(BaseResult<List<PlanMeetEntity>> listBaseRequestResult) {


                        mPlanMeetEntities.addAll(listBaseRequestResult.getResultMessage());


                    }

                    @Override
                    public void onError(Throwable e) {
                        mPlanMeetAdapter.notifyDataSetChanged();
                        super.onError(e);

                    }

                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                        mPlanMeetAdapter.notifyDataSetChanged();
                    }
                });
    }


}
